$(document).ready(function () {
    class Cart {
        constructor() {}
        static reduceNum(index) {
            let goods = Goods.find(index);
            let $item = $(`.shop_item[data-index=${index}]`);
            goods.reduce();
            $item.find('.goodsNum').val(goods.num);
            $item.find('.total').text(Goods.locale(goods.total));
        }
        static addNum(index) {
            let goods = Goods.find(index);
            let $item = $(`.shop_item[data-index=${index}]`);
            goods.add();
            $item.find('.goodsNum').val(goods.num);
            $item.find('.total').text(Goods.locale(goods.total));
        }
        static removeItem(index) {
            let $item = $(`.shop_item[data-index=${index}]`);
            $item.remove();
            Goods.remove(index);
        }
        static setAllTotal(allTotal = Goods.locale(Goods.countAllTotal())) {
            console.log(Goods.locale(Goods.countAllTotal()));
            console.log(allTotal);
            $('.allTotal').text(allTotal)
        }
        static emptyCart() {
            $('.shop_no').show();
            $('.shop_have').hide();
            $('.shop_total').hide();
        }
        static renderCart(data) {
            $('.shop_no').hide();
            $('.shop_have').show();
            $('.shop_total').show();
            let str = "";
            data.forEach((item, index) => {
                let goods = new Goods(index, item);
                str += `<div class="shop_item clear_fix" data-index="${index}">
                <div class="shop_ti_intro clear_fix"></div>
                <div class="shop_cart clear_fix">
                    <div class="cart_name">
                        <div class="cart_pic"><span class="cart_q" style="display: none;"><span class="cart_txt">缺货</span></span>
                            <img src="img/${goods.cover}">
                        </div>
                        <div class="cart_com">
                            <div class="cart_title">
                                ${goods.title}
                            </div>
                        </div>
                    </div>
                    <div class="cart_price">
                        <div class="c_price">RMB <span>${Goods.locale(goods.price)}</span></div>
                    </div>
                    <div class="cart_num">
                        <div class="num_m clear_fix">
                            <a href="javascript:void(0)" rel="nofollow" class="reduce">-</a>
                            <span class="num_p">
                                <input type="text" class="goodsNum" disabled="disabled" value="${goods.num}">
                            </span>
                            <a href="javascript:void(0)" rel="nofollow" class="add">+</a>
                        </div>
                    </div>
                    <div class="cart_total">
                        <div class="c_tto_p">
                            <div class="lt_total">RMB <span class="total">${Goods.locale(goods.total)}</span></div>
                        </div>
                    </div>
                    <div class="cart_opera">
                        <a href="javascript:void(0)" rel="nofollow" class="topool">移入心愿单</a>
                        <a href="javascript:void(0)" rel="nofollow" class="cp_del">删除</a>
                    </div>
                </div>
            </div>`
            });
            console.log(Goods.goodsArr);
            $('.shop_con').html(str);
            Goods.countAllTotal();
        }
    }
    if (!sessionStorage.uid) {
        Cart.emptyCart();
        return;
    }
    $.ajax({
        type: "post",
        url: "/searchCart",
        data: {
            uid: sessionStorage.uid
        },
    }).done(function (res) {
        let data = res.data;
        if (data.length == 0) {
            console.log("购物车为空");
            Cart.emptyCart(data);
        } else {
            Cart.renderCart(data);
            Cart.setAllTotal();

            $('.shop_con').on('click', '.reduce', function () {
                Cart.reduceNum(this.closest('.shop_item').dataset.index);
                Cart.setAllTotal();
            })

            $('.shop_con').on('click', '.add', function () {
                Cart.addNum(this.closest('.shop_item').dataset.index);
                Cart.setAllTotal();
            })

            $('.shop_con').on('click', '.cp_del', function () {
                Cart.removeItem(this.closest('.shop_item').dataset.index)
                Cart.setAllTotal();
            })

        }
    });
})